
//let requestUrl="http://192.168.1.15:8092/"
let requestUrl="http://127.0.0.1:8092/"
$(document).ready(function() {

    /* layer.open({
            type: 1 //Page层类型
            ,area: ['1000px', '600px']
            ,title: '公告须知'
            ,shade: 0.6 //遮罩透明度
            ,maxmin: true //允许全屏最小化
            ,anim: 1 //0-6的动画形式，-1不开启
            ,content: '<div style="padding:50px;">今日公告：请按时吃饭<p  style="color: red">332323</p></div>'
        });*/

    searchMain();

});

$("#searchMainMes").click(function () {

    searchMain();

});

$("#status0").click(function () {

    searchMain();
});
$("#status1").click(function () {

    searchMain();
});
$("#status2").click(function () {

    searchMain();
});
$("#status3").click(function () {

    searchMain();
});
function searchMain(){

    layui.use('table', function(){
        let ticket_state = "";
        let status0 = $("#status0").prop("className");
        let status1 = $("#status1").prop("className");
        let status2 = $("#status2").prop("className");
        let status3 =  $("#status3").prop("className");
        if(""!=status0 && null!=status0 && "layui-this"==status0){
            ticket_state="";
        }else if(""!=status1 && null!=status1&& "layui-this"==status1){

            ticket_state="审核中";
        }else if(""!=status2 && null!=status1&& "layui-this"==status2){

            ticket_state="打印失败";
        }else if(""!=status3 && null!=status1&& "layui-this"==status3){

            ticket_state="打印成功";
        }

        let travel_itinerary_id = $("#travel_itinerary_id").val();
        let ticket_number = $("#ticket_number").val();

        let ticket_style = $("#ticket_style").val();
        //alert(ticket_state);
        let data = {travel_itinerary_id:travel_itinerary_id,ticket_number:ticket_number,ticket_state:ticket_state,
            ticket_style:ticket_style}


        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:requestUrl+"getTickInfo"
            ,where:data
            ,method:"post"
            ,contentType:'application/json'
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.result_code, //解析接口状态
                    "msg": res.result_code, //解析提示文本
                    "count": res.result_data_size, //解析数据长度
                    "data": res.result_data //解析数据列表
                };
            }
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'serial_number', title:'序号', width:60, fixed: 'left', unresize: true, templet:'#serial_number'}
                ,{field:'travel_itinerary_id', title:'订单号', width:210}
                ,{field:'ticket_number', title:'行程单票号', width:160}
                ,{field:'ticket_money', title:'单价', width:130, sort: true}
                ,{field:'ticket_allmoney', title:'总价', width:130}
                ,{field:'ticket_style', title:'行程单类别',width:130}
                ,{field:'ticket_state', title:'行程单状态', width:130, sort: true}
                /*,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}*/
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        /*    table.on('tool(test)', function(obj){
                var data = obj.data;
                //console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        obj.del();
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer.prompt({
                        formType: 2
                        ,value: data.email
                    }, function(value, index){
                        obj.update({
                            email: value
                        });
                        layer.close(index);
                    });
                }
            });*/
    });

}







//下载
$("#downfile").click(function () {

    layer.msg('下载中，请稍后……');

});
//手动添加票号
$("#addInternationalTicket").click(function () {

    layer.open({
        type: 1 //Page层类型
        ,area: ['700px', '430px']
        ,title: '添加票号'
        ,shade: 0.6 //遮罩透明度
        ,maxmin: false //允许全屏最小化
        ,anim: 1 //0-6的动画形式，-1不开启
        ,content: contentMes
    });

    layui.use('form', function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();
    });

    $("#addTicket").click(function () {

        let ticket_style = $("#ticket_style").val();
        if(!ticket_style){
            layer.msg('票号类型不能为空', function(){
                $("#ticket_style").focus();

            });
            return false;
        }
        let ticket_number = $("#ticket_number").val();
        let ticket_number_test = /^\d{13}$/;
        if(!ticket_number){
            layer.msg('票号不能为空', function(){
                $("#ticket_number").focus();

            });
            return false;
        }else if(!ticket_number_test.test(ticket_number)){

            layer.msg('请输入正确的票号', function(){
                $("#ticket_number").focus();

            });
            return false;
        }
        let ticket_money = $("#ticket_money").val();
        if(!ticket_money){
            layer.msg('价格不能为空', function(){
                $("#ticket_money").focus();

            });
            return false;
        }
        let ticket_allmoney = $("#ticket_allmoney").val();
        if(!ticket_allmoney){
            layer.msg('总价不能为空', function(){
                $("#ticket_allmoney").focus();

            });
            return false;
        }
        let data = {ticket_style:ticket_style,ticket_number:ticket_number,ticket_money:ticket_money,ticket_allmoney:ticket_allmoney}
        data = JSON.stringify(data);
        $.ajax({

            type:"POST",
            url:requestUrl+"insertTick",
            data:data,
            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
            contentType:"application/json;charset=UTF-8",
            beforeSend:function(){

            },

            success:function(data){
                //alert(data.result_code==0);
                if(data.result_code==0){

                    layer.load(2);
                    layer.msg(data.result_mes);
                    setTimeout(function () {
                        layer.closeAll();
                    },1000);

                }else{
                    layer.msg(data.result_mes);

                }

            },

            complete: function(XMLHttpRequest, textStatus){
                //alert(XMLHttpRequest.responseText);
                //alert(textStatus);
                //HideLoading();
            },
            //调用出错执行的函数
            error: function(){
                layer.msg('系统出错', function(){

                    return false;
                });
            }
        });




    });
});


//导入文件
layui.use('upload', function(){
    var $ = layui.jquery
        ,upload = layui.upload;

    //指定允许上传的文件类型
    upload.render({
        elem: '#test3'
        ,url: requestUrl+"importExcel"
        ,accept: 'file' //普通文件
        ,exts: 'xls|xlsx'
        ,done: function(res){
            var reusultMes = res;

            //alert(reusultMes.result_code);
            showExcelMes(reusultMes);


            console.log(res);
        }
    });
});


//导入文件后展示结果
function showExcelMes(reusultMes){

    //alert(reusultMes.result_code);
    let excelTableMes = "<table class=\"layui-hide\" id=\"excelTableMes\" lay-filter=\"test\"></table>"
        +"<script type=\"text/html\" id=\"toolbarDemo\">\n" +
        "    <div class=\"layui-btn-container\">\n" +
        "        <button class=\"layui-btn layui-btn-sm\" id=\"submitMes\">确认提交</button>\n" +
        "        <button class=\"layui-btn layui-btn-sm\" id=\"cancleMes\">取消/重新导入</button>\n" +
        "    </div>\n" +
        "</script>";
    layer.open({
        type: 1 //Page层类型
        ,area: ['700px', '430px']
        ,title: '票号信息展示'
        ,shade: 0.6 //遮罩透明度
        ,maxmin: false //允许全屏最小化
        ,anim: 1 //0-6的动画形式，-1不开启
        ,content: excelTableMes
    });

    var table = layui.table;
    layui.use('table', function(){
    table.render({
        elem: '#excelTableMes'
        /*,url:requestUrl+"importExcel"*/
        ,contentType:'application/json'
        ,data:reusultMes.result_data
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,defaultToolbar: ['filter', 'exports', 'print']
        ,title: '用户数据表'
        ,cols: [[
            {type: 'numbers', fixed: 'left',title: '序号',width:80}
            ,{field:'ticket_number', title:'行程单票号', width:180}
            ,{field:'ticket_money', title:'单价', width:130, edit: 'text', sort: true}
            /*,{field:'ticket_allmoney', title:'总价', width:130}*/
            ,{field:'ticket_style', title:'行程单类别',width:130}
        ]]
        ,page: true
        ,done:function () {


        }
    });

    });

    $("#submitMes").click(function () {
        //reusultMes = JSON.stringify(reusultMes);
        //alert(reusultMes.result_code);
        //alert(reusultMes.result_mes);
       // alert(reusultMes.result_data);
        let resultData = JSON.stringify(reusultMes.result_data);

        let requestMes = {result_data:resultData};
        requestMes = JSON.stringify(requestMes);
        //alert(requestMes);
        //alert(JSON.stringify(reusultMes.result_data[0]));
        $.ajax({

            type:"POST",
            async : true,
            url:requestUrl+"insertMuchTick",
            data:requestMes,
            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
            contentType:"application/json;charset=UTF-8",
            beforeSend:function(){

            },

            success:function(data){
                //alert(data.result_code==0);
                if(data.result_code==0){

                    layer.load(2);
                    layer.msg(data.result_mes);
                    setTimeout(function () {
                        layer.closeAll();
                    },1000);

                }else{
                    layer.msg(data.result_mes);

                }
            },
            complete: function(XMLHttpRequest, textStatus){
            },
            error: function(){
                layer.msg('系统出错', function(){
                    return false;
                });
            }
        });

    });
    $("#cancleMes").click(function () {

        layer.closeAll();

    });

}


//添加票号弹出框
let contentMes="<div style=\"text-align: center;\">\n" +
    "<form class=\"layui-form\" lay-filter=\"test1\" action=\"\" >\n" +
    "\n" +
    "    <div class=\"layui-inline\"></div>\n" +
    "    <div class=\"layui-form-item\">\n" +
    "        <div class=\"layui-inline\">\n" +
    "            <label class=\"layui-form-label\">行程单类别</label>\n" +
    "            <div class=\"layui-input-inline\">\n" +
    "                <select name=\"modules\" lay-verify=\"required\" lay-search=\"\" id=\"ticket_style\">\n" +
    "                    <option value=\"\">直接选择或搜索选择</option>\n" +
    "                    <option value=\"国际\">国际</option>\n" +
    "                    <option value=\"国内\">国内</option>\n" +
    "                </select>\n" +
    "            </div>\n" +
    "        </div>\n" +
    "    </div>\n" +
    "\n" +
    "\n" +
    "    <div class=\"layui-form-item\">\n" +
    "        <div class=\"layui-inline\">\n" +
    "            <label class=\"layui-form-label\">票号</label>\n" +
    "            <div class=\"layui-input-inline\">\n" +
    "                <input type=\"tel\" name=\"phone\"  autocomplete=\"off\" class=\"layui-input\" id=\"ticket_number\">\n" +
    "            </div>\n" +
    "        </div>\n" +
    "    </div>\n" +
    "\n" +
    "    <div class=\"layui-form-item\">\n" +
    "        <div class=\"layui-inline\">\n" +
    "            <label class=\"layui-form-label\">价格</label>\n" +
    "            <div class=\"layui-input-inline\">\n" +
    "                <input type=\"text\" name=\"number\"  autocomplete=\"off\" class=\"layui-input\" id=\"ticket_money\">\n" +
    "            </div>\n" +
    "        </div>\n" +
    "\n" +
    "    </div>\n" +
    "    <div class=\"layui-form-item\">\n" +
    "        <div class=\"layui-inline\">\n" +
    "            <label class=\"layui-form-label\">总价</label>\n" +
    "            <div class=\"layui-input-inline\">\n" +
    "                <input type=\"text\" name=\"number\" autocomplete=\"off\" class=\"layui-input\" id=\"ticket_allmoney\">\n" +
    "            </div>\n" +
    "        </div>\n" +
    "\n" +
    "    </div>\n" +
    "\n" +
    "\n" +
    "    <div class=\"layui-form-item\" style=\"width:100px;margin-left:215px \">\n" +
    "        <div class=\"layui-input-block\">&nbsp;\n" +
    "            <button type=\"submit\" class=\"layui-btn\" lay-submit=\"\" lay-filter=\"demo1\" id=\"addTicket\">立即提交</button>\n" +
    "        </div>\n" +
    "    </div>\n" +
    "</form>\n" +
    "</div>";